/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.service.beans;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;


/**
 * This is the top level element of the operators_conf.xml.
 * It allows to define search operators.
 *
 */
public class JBOperators
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBOperators.class);

    /** List of JBOperator. */
    private Map operatorsMap = new HashMap();

    /** Message properties file for operators labels. */
    private String resourcename;

    /** Resource bundle for operators labels. */
    private ResourceBundle opResourceBundle;

    /**
     * Adds an operator.
     *
     * @param op operator to add
     */
    public void addOperator(JBOperator op)
    {
        op.compute(opResourceBundle);
        operatorsMap.put(op.getName().trim().toLowerCase(), op);
    }

    /**
     * Gets an operator from its name.
     *
     * @param name operator name
     *
     * @return operator
     */
    public JBOperator findOperatorByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (JBOperator) operatorsMap.get(key);
    }

    /**
     * Displays operators configuration.
     *
     * @return String formatted operators configuration
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;
        buf.append("   resourcename = ");
        buf.append(resourcename);
        buf.append(System.getProperty("line.separator"));

        if (operatorsMap != null)
        {
            buf.append("Map components of operatorsMap = ");
            buf.append(System.getProperty("line.separator"));
            it = operatorsMap.values().iterator();

            while (it.hasNext())
            {
                buf.append(".");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    /**
     * Returns the name of the message properties file for operators labels.
     * @return file name.
     */
    public String getResourcename()
    {
        return resourcename;
    }

    /**
     * Sets the name of the message properties file for operators labels..
     * @param aresourcename The file name to set
     */
    public void setResourcename(String aresourcename)
    {
        this.resourcename = aresourcename;

        /*Chargement du ResourceBundle*/
        try
        {
            opResourceBundle = ResourceBundle.getBundle(resourcename);
            _logger.info(resourcename + " succesfully loaded");
        }
        catch (MissingResourceException mre)
        {
            _logger.error(
                ".. JBOperators.setResourcename() <resources not found>" +
                resourcename);
        }
    }
}
